Skip to main content

Selenium 4 New Features

Why Selenium 4 Matters (Real Project Context)​

Selenium 4 is not just a version bumpβ€”it modernizes Selenium to align with W3C WebDriver standard, improves stability, and adds developer-focused features that reduce flaky tests.

If you’re building new frameworks today, Selenium 4 is mandatory.


1. W3C WebDriver Compliance (Most Important)​

Selenium 4 is fully compliant with the W3C WebDriver standard.

Why This Matters​

  • Consistent behavior across browsers
  • Fewer browser-specific hacks
  • Better Grid and cloud compatibility

Real Impact​

  • Fewer random failures
  • More predictable interactions
  • Cleaner browser logs

2. Relative Locators (UI-Focused Feature)​

Locate elements based on their position relative to other elements.

WebElement password = driver.findElement(By.id("password"));
WebElement username = driver.findElement(
RelativeLocator.with(By.tagName("input")).above(password)
);

When Useful​

  • Forms without unique IDs
  • Dynamic layouts
  • Reducing complex XPath

⚠️ Use sparinglyβ€”IDs and stable locators are still preferred.


3. New Window & Tab Handling API​

Clear and explicit control for tabs/windows.

driver.switchTo().newWindow(WindowType.TAB);
driver.get("https://example.com");

Benefits​

  • No JavaScript hacks
  • Cleaner multi-window tests

4. Native Shadow DOM Support​

Selenium 4 provides native access to open Shadow DOM.

WebElement host = driver.findElement(By.cssSelector("custom-element"));
SearchContext shadowRoot = host.getShadowRoot();

Removes dependency on JavaScript workarounds.


5. Improved Actions API​

Actions are now more reliable and aligned with W3C input actions.

Actions actions = new Actions(driver);
actions.moveToElement(element).click().perform();

Especially improves:

  • Hover
  • Drag-and-drop
  • Slider interactions

6. Browser Options Unification​

Options classes now replace DesiredCapabilities.

ChromeOptions options = new ChromeOptions();
options.setAcceptInsecureCerts(true);

Cleaner, safer, Selenium 4–only approach.


7. Better Error Messages & Logging​

Selenium 4 provides:

  • Clearer stack traces
  • Better timeout messages
  • Improved debugging experience

This directly reduces debugging time in CI.


8. DevTools Integration (Awareness)​

Limited Chrome DevTools Protocol (CDP) integration.

Use cases:

  • Network interception (basic)
  • Performance metrics

⚠️ Advanced CDP usage is out of scope for most UI automation.


What Selenium 4 Does NOT Change​

❌ Selenium is still not a visual testing tool
❌ Selenium does not test APIs
❌ Selenium does not replace frameworks


Common Migration Mistakes βŒβ€‹

  1. Continuing to use DesiredCapabilities
  2. Assuming Selenium 4 fixes bad waits
  3. Overusing relative locators
  4. Ignoring Shadow DOM changes
  5. Not upgrading browser drivers

Best Practices βœ…β€‹

  • Use Selenium 4 APIs only
  • Remove deprecated patterns
  • Update Grid configuration
  • Validate framework compatibility
  • Train team on new features

Interview Notes πŸŽ―β€‹

Q: Biggest improvement in Selenium 4?
A: Full W3C WebDriver compliance.

Q: Are DesiredCapabilities used in Selenium 4?
A: No, they are deprecated.

Q: Does Selenium 4 support Shadow DOM?
A: Yes, open Shadow DOM natively.


Real-Project Tip πŸ’‘β€‹

Selenium 4 improves stabilityβ€”but only if your framework design and waits are correct.


Summary​

  • Selenium 4 aligns with modern browsers
  • Adds powerful UI-focused features
  • Removes legacy APIs
  • Essential for enterprise automation